package cn.iocoder.yudao.module.biz.controller.admin.tencent;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "腾讯地图接口")
@RestController
@RequestMapping("/tencent/map")
@Validated
public class TencentMapController {

    @Value("${tencent.map.key}")
    private String mapKey;

    @GetMapping("/reverseGeocode")
    @Operation(summary = "逆地址解析", description = "根据经纬度获取地址")
    public String reverseGeocode(@RequestParam Double lat, @RequestParam Double lng) throws Exception {
        String url = "https://apis.map.qq.com/ws/geocoder/v1/?location="+ lat + "," + lng + "&key=" + mapKey;
        String resp = HttpUtil.get(url);
        if(null != resp){
            JSONObject jsonObject = JSONObject.parseObject(resp);
            JSONObject result = jsonObject.getJSONObject("result");
            if(null != result) {
                return result.toJSONString();
            }else{
                return null;
            }
        }else{
            return null;
        }
    }
}